package com.joven.product.api.business.factory;

import com.google.common.collect.Lists;
import com.joven.infra.common.model.PageResult;
import com.joven.infra.common.util.CheckUtils;
import com.joven.product.api.business.client.ProductClient;
import com.joven.product.api.business.client.SupplierClient;
import com.joven.product.api.business.client.UserClient;
import com.joven.product.api.business.client.model.*;
import com.joven.product.api.business.exception.ExceptionEnum;
import com.joven.product.api.business.service.dto.*;
import com.joven.product.api.infrastructure.converter.ProductConverter;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品工厂
 *
 * @author Joven
 * @date 2022/6/28 15:25
 */
@Component
public class ProductFactory {
    @Autowired
    private SupplierClient supplierClient;
    @Autowired
    private ProductClient productClient;
    @Autowired
    private UserClient userClient;

    public ProductCreateCmd build(ProductCreateDTO dto) {
        Long supplierId = dto.getSupplierId();
        Supplier supplier = supplierClient.getById(supplierId);
        CheckUtils.assertTrue(supplier != null, ExceptionEnum.SUPPLIER_NOT_EXISTS.createException());
        return ProductConverter.INSTANCE.convert(dto);
    }

    public ProductEditCmd build(ProductEditDTO dto) {
        Long supplierId = dto.getSupplierId();
        Supplier supplier = supplierClient.getById(supplierId);
        CheckUtils.assertTrue(supplier != null, ExceptionEnum.SUPPLIER_NOT_EXISTS.createException());
        return ProductConverter.INSTANCE.convert(dto);
    }

    public PageResult<ProductDTO> getByPage(ProductPageQueryDTO dto) {
        PageResult<Product> page = productClient.getByPage(ProductConverter.INSTANCE.convert(dto));
        PageResult<ProductDTO> pageResult = (PageResult) page;
        if (CollectionUtils.isEmpty(pageResult.getRecords())) {
            return pageResult;
        }
        List<Long> ids = page.getRecords().stream()
                .map(it -> Lists.newArrayList(it.getCreateUser(), it.getUpdateUser()))
                .flatMap(Collection::stream).collect(Collectors.toList());
        Map<Long, String> userMap = userClient.listByIds(ids)
                .stream().collect(Collectors.toMap(User::getId, User::getName));
        List<ProductDTO> list = page.getRecords().stream()
                .map(it -> {
                    ProductDTO product = ProductConverter.INSTANCE.convert(it);
                    product.setCreateUserName(userMap.get(it.getCreateUser()));
                    product.setUpdateUserName(userMap.get(it.getUpdateUser()));
                    return product;
                }).collect(Collectors.toList());
        pageResult.setRecords(list);
        return pageResult;
    }

    public ProductDetailDTO getDetailById(Long id) {
        ProductDetail product = productClient.getDetailById(id);
        if (product == null) {
            return null;
        }
        Map<Long, String> userMap = userClient.listByIds(Lists.newArrayList(product.getCreateUser(), product.getUpdateUser()))
                .stream().collect(Collectors.toMap(User::getId, User::getName));
        ProductDetailDTO dto = ProductConverter.INSTANCE.convert(product);
        dto.setCreateUserName(userMap.get(dto.getCreateUser()));
        dto.setUpdateUserName(userMap.get(dto.getUpdateUser()));
        return dto;
    }
}
